package com.qiyun.dinghuohui.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.qiyun.dinghuohui.common.ClientCookie;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class AsyncImageTask extends AsyncTask<String, String, Bitmap> {
	ImageView view;
	Drawable defaultDrawable;

	public AsyncImageTask(ImageView view, Drawable defaultDrawable) {
		this.view = view;
		this.defaultDrawable = defaultDrawable;
	}

	private Bitmap readImage(String... params) {
		try {
			HttpClient conn_client = null;
			HttpGet conn_get = null;
			HttpResponse responsefordown;
			URL myFileUrl = null;
			Bitmap bitmap = null;
			InputStream is = null;
			HttpURLConnection conn = null;
			try {
				conn_client = new DefaultHttpClient();
				conn_get = new HttpGet(ClientCookie.getServerUrl() + params[0]);
				conn_get.addHeader("Connection", "close");
				responsefordown = conn_client.execute(conn_get);
				if (responsefordown.getStatusLine().getStatusCode() == 200) {
					HttpEntity entity = responsefordown.getEntity();
					is = entity.getContent();
					bitmap = BitmapFactory.decodeStream(is);
				}
				/*
				 * myFileUrl = new URL(ClientCookie.getServerUrl() + params[0]);
				 * conn = (HttpURLConnection) myFileUrl.openConnection();
				 * conn.setRequestMethod("GET"); conn.setUseCaches(false);
				 * conn.setConnectTimeout(20*1000);//连接主机的超时时间（单位：毫秒）
				 * //conn.setReadTimeout(30*1000);//从主机读取数据的超时时间（单位：毫秒）
				 * conn.setDoInput(true); conn.connect(); is =
				 * conn.getInputStream(); bitmap =
				 * BitmapFactory.decodeStream(is); conn.disconnect(); conn=null;
				 */
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (conn_client != null) {
						conn_client.getConnectionManager().shutdown();
					}
					if (is != null) {
						is.close();
					}
					if (conn != null) {
						conn.disconnect();
						conn = null;
					}
					myFileUrl = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return bitmap;
		} catch (Exception e) {
		}
		return null;
	}

	@Override
	protected Bitmap doInBackground(String... params) {
		Bitmap bitmap = readImage(params);
		if (bitmap == null) {
			bitmap = readImage(params);
		}
		return bitmap;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		// if (result != null && DeviceUtils.checkSDCard()) {
		// File f = new File("/sdcard/" + 1 + ".png");
		// FileOutputStream fOut = null;
		// try {
		// f.createNewFile();
		// fOut = new FileOutputStream(f);
		// result.compress(Bitmap.CompressFormat.PNG, 100, fOut);
		// } catch (IOException e) {
		// e.printStackTrace();
		// } finally {
		// if (fOut != null) {
		// try {
		// fOut.close();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// }
		// }
		// }

		if (result == null) {
			// 没有找到图片,设置默认图片
			view.setImageDrawable(defaultDrawable);
		} else {
			view.setImageBitmap(result);
		}
	}

}
